

package com.bei_en.teacher.admin.service.questions;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.admin.service.BaseService;
import com.bei_en.teacher.admin.model.questions.bean.Questions;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsCondition;
import com.bei_en.teacher.admin.persistence.dao.questions.QuestionsDao;

/**
 * 
 * @author Guocg
 * @date 2016-8-15 23:28:11
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class QuestionsService extends BaseService{

    @Autowired
    private QuestionsDao questionsDao;

    public void insert(QuestionsVo model) {

    	 questionsDao.insert(model.getQuestions());
    }

//    /**
//     * 插入并返回生成的id
//     * @param model
//     * @return
//     */
//    public Integer merge(QuestionsVo model) {
//
//	questionsDao.insert(model.getQuestions());
//	return questionsDao.findLastInsertedId();
//
//    }

    public void delete(Integer modelInteger) {

    	questionsDao.delete(modelInteger);
    }

    public QuestionsVo findById(Integer modelInteger) {

    	QuestionsVo model = new QuestionsVo(questionsDao.findById(modelInteger));
    	return model;
    }

    public void update(QuestionsVo model) {

    	questionsDao.update(model.getQuestions());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(QuestionsVo model) {

    	questionsDao.updateSelective(model.getQuestions());
    }

    public int countAll() {

    	return questionsDao.countAll();
    }

    public List<QuestionsVo> findAll() {

		List<QuestionsVo> list = new ArrayList<QuestionsVo>();
		List<Questions> list1 = questionsDao.findAll();
		for (Questions model : list1) {
		    list.add(new QuestionsVo(model));
		}
		return list;
    }

    public List<Integer> findAllIds() {

    	return questionsDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<QuestionsVo> findQuestionsPage(QuestionsCondition vo) {

		Integer totalCount = searchQuestionsNum(vo);
		List<QuestionsVo> list = new ArrayList<QuestionsVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<Questions> list1 = questionsDao.findPageBreakByCondition(vo, rb);
		    for (Questions model : list1) {
		    	list.add(new QuestionsVo(model));
		    }
	
		}

		return list;
    }

    private Integer searchQuestionsNum(QuestionsCondition Vo) {

    	return questionsDao.findNumberByCondition(Vo);
    }
    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<QuestionsVo> findQuestionsPage3(QuestionsCondition vo) {

		Integer totalCount = searchQuestionsNum(vo);
		List<QuestionsVo> list = new ArrayList<QuestionsVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<Questions> list1 = questionsDao.findPageBreakByCondition3(vo, rb);
		    for (Questions model : list1) {
		    	list.add(new QuestionsVo(model));
		    }
	
		}

		return list;
    }

    private Integer searchQuestionsNum3(QuestionsCondition Vo) {

    	return questionsDao.findNumberByCondition3(Vo);
    }
    /**
     * 分页查询，包括总数和内容   题库和type的集合
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<QuestionsVo> findQuestionsPage2(QuestionsCondition vo) {

		Integer totalCount = searchQuestionsNum2(vo);
		List<QuestionsVo> list = new ArrayList<QuestionsVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<Questions> list1 = questionsDao.findPageBreakByCondition2(vo, rb);
		    for (Questions model : list1) {
		    	list.add(new QuestionsVo(model));
		    }
	
		}

		return list;
    }

    private Integer searchQuestionsNum2(QuestionsCondition Vo) {

    	return questionsDao.findNumberByCondition2(Vo);
    }
}
